/**********************************************************************************************/
/* FBL_Sort.cpp																				  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006                                        					  */
/* All Rights Reserved.                                                 					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/Headers/FBL.h>


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
#define Swap(left,right)	\
	theTmp 	= *left;		\
	*left 	= *right;		\
	*right 	= theTmp;

void qsort_long( ulong* first, ulong* last ) ;                    
 
 
/**********************************************************************************************/
void qsort_long( ulong* first, ulong* last )                      
{
	ulong*	lp;
	ulong*	rp;
	ulong*	ip;
	ulong*	jp;
	ulong	theTmp;
	
	if( first == last )
		return;
	
	ulong theCount = (ulong)(last - first + 1);
	
	rp = last;	
	lp = first + theCount/2;	
	
	for (;;)
	{
		if (lp > first)
			--lp; 
		else{
			Swap(lp,rp);
			
			--rp; 
			if (rp == first)
				return;
		}
			
		jp = lp;
		
		while( jp + (jp - first + 1) <= rp )
		{
			ip = jp;
			jp += jp - first + 1;	// jp *= 2; 
			
			if( jp < rp ){
				if ( *jp < *(jp+1) )
					++jp; 
			}
			
			if( *ip < *jp ){	
					Swap(ip,jp);
			}else	break;
		}
	}
}


/**********************************************************************************************/
FBL_End_Namespace
